/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "group.h"

#include <QXmlStreamReader>

#include "../utils.h"

QDebug operator<<(QDebug dbg, const Group& g)
{
    dbg << "Group(\n";
    dbg << "Id: " << g.id <<  " Name: " << g.name << "\n";
    dbg << "Website: " << g.website << "\n";
    dbg << "PouetId: " << g.pouetId << "\n";
    dbg << "Artists: " << g.listArtist << "\n";
    dbg << "Songs: " << g.listSongs << "\n";
    dbg << ")\n";

    return dbg.space();
}

bool parseGroup(QXmlStreamReader& xmlReader, Group& g)
{
    QXmlStreamReader::TokenType nodeType = xmlReader.tokenType();
    bool isOk = true;

    qDebug() << "parseGroup";

    while ( (nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "group") && isOk )
    {
        if ( nodeType == QXmlStreamReader::StartElement )
        {
            if ( xmlReader.name() == "group" )
            {
                g.id = xmlReader.attributes().value("id").toString().toUInt();
                isOk &= Utils::getXMLStringData(xmlReader,g.name);
            }
            else
            {
                qDebug() << "One tag is not parsed at all -> " << xmlReader.name();
            }
        }

        nodeType = xmlReader.readNext();
    }

    qDebug() << g;

    return isOk;
}

bool parseGroupPage(QXmlStreamReader& xmlReader, Group& g)
{
    QXmlStreamReader::TokenType nodeType = xmlReader.tokenType();
    bool isOk = true;

    qDebug() << "parseGroupPage";

    while ( (nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "group") && isOk )
    {
        if ( nodeType == QXmlStreamReader::StartElement )
        {
            if ( xmlReader.name() == "group" )
            {
                g.id = xmlReader.attributes().value("id").toString().toUInt();
            }
            else if ( xmlReader.name() == "name" )
            {
                isOk &= Utils::getXMLStringData(xmlReader,g.name);
            }
            else if ( xmlReader.name() == "website" )
            {
                QString urlString;
                isOk &= Utils::getXMLStringData(xmlReader,urlString);
                if ( isOk )
                {
                    g.website = QUrl(urlString);
                }
            }
            else if ( xmlReader.name() == "pouet")
            {
                QString pouetIdString;
                isOk &= Utils::getXMLStringData(xmlReader,pouetIdString);
                if ( isOk )
                {
                    g.pouetId = pouetIdString.toUInt(&isOk);
                }
            }
            else if ( xmlReader.name() == "active_group_members")
            {
                while ( (nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "active_group_members") && isOk )
                {
                    if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "artist" )
                    {
                        Artist a;
                        isOk &= parseArtist(xmlReader,a);
                        g.listArtist.push_back(a);
                    }

                    nodeType = xmlReader.readNext();
                }
            }
            else if ( xmlReader.name() == "active_group_songs")
            {
                while ( (nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "active_group_songs") && isOk )
                {
                    if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "song" )
                    {
                        Song s;
                        isOk &= parseSong(xmlReader,s);
                        g.listSongs.push_back(s);
                    }

                    nodeType = xmlReader.readNext();
                }
            }
            else
            {
                qDebug() << "One tag is not parsed at all -> " << xmlReader.name();
            }
        }

        nodeType = xmlReader.readNext();
    }

    qDebug() << g;

    return isOk;
}

bool readGroupPage(const QByteArray& streamData, Group& g)
{
    QXmlStreamReader xmlReader(streamData);
    QXmlStreamReader::TokenType nodeType;
    bool isOk = true;

    qDebug() << "readGroupPage";

    while ( !xmlReader.atEnd() )
    {
        nodeType = xmlReader.readNext();

        if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "group" )
        {
            isOk &= parseGroupPage(xmlReader,g);
        }
    }

    return isOk;
}
